package elitech.vietnam.weatherfashion.backgroundtask;

import java.io.IOException;

import android.content.Context;
import android.os.AsyncTask;
import android.widget.Toast;
import elitech.vietnam.weatherfashion.R;
import elitech.vietnam.weatherfashion.WFApplication;
import elitech.vietnam.weatherfashion.config.GlobalValue;
import elitech.vietnam.weatherfashion.dto.MemberDTO;
import elitech.vietnam.weatherfashion.dto.PostDTO;
import elitech.vietnam.weatherfashion.service.PHPSoapServiceCaller;
import elitech.vietnam.weatherfashion.widget.LikeButton;

public class PostLikeTask extends AsyncTask<Integer, Integer, Boolean> {
	
	LikeButton 	button;
	PostDTO 	post;
	boolean 	liked;
	Context 	context;
	
	public PostLikeTask(Context context, LikeButton button, PostDTO post, boolean liked) {
		this.button = button;
		this.post 	= post;
		this.liked 	= liked;
		this.context = context;
	}

	@Override
	protected Boolean doInBackground(Integer... params) {
		PHPSoapServiceCaller service = new PHPSoapServiceCaller();
		try {
			MemberDTO user = GlobalValue.getInstance(context)
					.readUserInfo();
			return service.like(user.getId(), post.getId(), liked);
		} catch (IOException e) {
			return false;
		}
	}
	
	@Override
	protected void onPostExecute(Boolean result) {
		//button.setEnabled(true);
		if (!result) {
			
//						AlertDialog.Builder bulder = new AlertDialog.Builder(activity);
//						bulder.setTitle(activity.getString(R.string.confirm_exit)).setMessage(activity.getString(R.string.post_task_failed)).setPositiveButton(activity.getString(R.string.close), null).create()
//								.show();
			Toast.makeText(context, context.getString(R.string.post_task_failed), Toast.LENGTH_LONG).show();
			post.setLikes((liked) ? post.getLikes() - 1 : post.getLikes() + 1);
			post.setLiked(!liked);
			updateGlobalData();
		}
		button.setData(post.getLikes(), post.isLiked());
		
		this.button = null;
		this.context = null;
	}
	
	@Override
	protected void onPreExecute() {
		//button.setEnabled(false);
		post.setLikes((!liked) ? post.getLikes() - 1 : post.getLikes() + 1);
		post.setLiked(liked);
		button.setData(post.getLikes(), liked);
		updateGlobalData();
	}
	
	private void updateGlobalData() {
		PostDTO today = null, styler1 = null, styler2 = null, self = null;
		
		if (WFApplication.getInstance().todayData != null && !WFApplication.getInstance().todayData.contains(post))
			for (PostDTO p : WFApplication.getInstance().todayData) 
				if (p.getId() == post.getId()) {
					today = p;
					break;
				}
		
		if (WFApplication.getInstance().stylerAllData != null && !WFApplication.getInstance().stylerAllData.contains(post))
			for (PostDTO p : WFApplication.getInstance().stylerAllData) 
				if (p.getId() == post.getId()) {
					styler1 = p;
					break;
				}
			
		if (WFApplication.getInstance().stylerFriendData != null && !WFApplication.getInstance().stylerFriendData.contains(post))
			for (PostDTO p : WFApplication.getInstance().stylerFriendData) 
				if (p.getId() == post.getId()) {
					styler2 = p;
					break;
				}
		
		if (WFApplication.getInstance().selfData != null && !WFApplication.getInstance().selfData.contains(post))
			for (PostDTO p : WFApplication.getInstance().selfData) 
				if (p.getId() == post.getId()) {
					self = p;
					break;
				}
		
		if (today != null) {
			today.setLikes(post.getLikes());
			today.setLiked(post.isLiked());
		}
		
		if (styler1 != null) {
			styler1.setLikes(post.getLikes());
			styler1.setLiked(post.isLiked());
		}
		
		if (styler2 != null) {
			styler2.setLikes(post.getLikes());
			styler2.setLiked(post.isLiked());
		}
		
		if (self != null) {
			self.setLikes(post.getLikes());
			self.setLiked(post.isLiked());
		}
	}
}
